package com.fanshuai.config;

import org.gitlab.api.GitlabAPI;
import org.gitlab.api.models.GitlabBranch;
import org.gitlab.api.models.GitlabGroup;
import org.gitlab.api.models.GitlabProject;

import java.io.IOException;
import java.util.List;

public class GitlabUtils {
    private static String host = "https://gitlab.baicizhan.com/";
    private static String accessToken = "XZiz8say-yv6dAXhJ3Lp";

    static GitlabAPI gitlabAPI;
    static {
        gitlabAPI = GitlabAPI.connect(host, accessToken);
    }

    public static GitlabProject getProject(String group, String projectName) throws IOException {
        return gitlabAPI.getProject(group, projectName);
    }

    public static List<GitlabProject> getGroupProjects(int groupId) {
        GitlabGroup gitlabGroup = new GitlabGroup();
        gitlabGroup.setId(groupId);

        return gitlabAPI.getGroupProjects(gitlabGroup);
    }

    public static List<GitlabBranch> getAllBranches(GitlabProject gitlabProject) {
        return gitlabAPI.getBranches(gitlabProject);
    }

    public static GitlabBranch getBranch(GitlabProject gitlabProject, String branch) throws Exception {
        return gitlabAPI.getBranch(gitlabProject, branch);
    }

    public static void mergeCode(GitlabProject gitlabProject, String targetBranch, String commitId) throws IOException {
        gitlabAPI.createBranch(gitlabProject, targetBranch, commitId);
    }

}
